package cn.engage.org.service.impl;

import cn.engage.basic.PageList;
import cn.engage.org.domain.Department;
import cn.engage.org.mapper.DepartmentMapper;
import cn.engage.org.service.DepartmentService;
import cn.engage.query.DepartmentQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {
        @Autowired
        private DepartmentMapper departmentMapper;

        @Override
        @Transactional
        public void add(Department department) {
                departmentMapper.add(department);
        }

        @Override
        @Transactional
        public void update(Department department) {
                departmentMapper.update(department);
        }

        @Override
        @Transactional
        public void delete(Long id) {
                departmentMapper.delete(id);
        }

        @Override
        public List<Department> findAll() {
                return departmentMapper.findAll();
        }

        @Override
        public Department findById(Long id) {
                return departmentMapper.findById(id);
        }

        @Override
        public PageList<Department> queryByPage(DepartmentQuery query) {
                // 查询条数
                Integer totals = departmentMapper.queryByCount(query);
                if (null == totals || 0 == totals){
                        return new PageList<>(0,new ArrayList<>());
                }
                // 查询数据
                List<Department> departments = departmentMapper.queryByPage(query);
                //  放到返回里面
                return new PageList<Department>(totals,departments);
        }
}
